package com.offcn.mq.utils;

import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ld
 * @date 2021/9/18 15:59
 */
@Component
public class SmsUtil {

    @Value("${sms.accessKeyId}")
    private String accessKeyId;

    @Value("${sms.accessKeySecret}")
    private String accessKeySecret;


    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

    public SendSmsResponse  sendMsg(String phoneNum,String code) throws Exception {
        //java.util.List<String> args = java.util.Arrays.asList(args_);
        //调用方法
        com.aliyun.dysmsapi20170525.Client client = this.createClient(accessKeyId,
                accessKeySecret);
        Map<Object, Object> map = new HashMap<>();
        map.put("code", code);
        String codeStr = JSON.toJSONString(map);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNum)
                .setSignName("香榭购物")
                .setTemplateCode("SMS_181211097")
                .setTemplateParam(codeStr);
        //.setTemplateParam("{code:888888}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
       // System.out.println(sendSmsResponse.getBody());
        return sendSmsResponse;
    }

}
